// 网关配置的前缀
var jobsServer = "/jobsSer"

let isInterested = false;

let userId = "";
let CorHId = "";
let huntingId = "";
let interestedList = [];
let interviewList = [];

let Authorization = "";
let refreshToken = "";

// 加载页面时读取后台的 ip
window.onload = function () {
    $.ajaxSettings.async = false;
    $.getJSON("/js/properties.json", function (data) {
        jobsServer = data.jobsServer;
    });
    // 获取令牌
    Authorization = getCookie("Authorization")
    refreshToken = getCookie("refreshToken")
    userId = $("#userId").val();
    CorHId = $("#CorHId").val();
    huntingId = $("#huntingId").val();
    // 查询是否邀请面试
    getInterviewList();
    // 查询是否感兴趣
    getInterestedList();
    // 查询用户简历
    getProfileByUserId();
}

function getProfileByUserId() {
    $.ajax({
        type: "GET",
        async: false,
        url: jobsServer + "/account/profile?userId=" + userId,
        headers: {
            "Authorization": Authorization,
            "refreshToken": refreshToken,
        },
        success: function (data) {
            // console.log(data);

            let profile = data.datas
            // 基本信息
            let baseInfo = profile.baseInfo;
            $("#photo").attr("src", baseInfo.photo);
            $("#name").html(baseInfo.name);
            $("#industry").html(baseInfo.industry);
            $("#experience").html(baseInfo.experience);
            $("#birthday").html(baseInfo.birthday);
            $("#sex").html(baseInfo.sex);
            $("#salary").html(baseInfo.salary);
            if (baseInfo.selfEvaluation === "" ||
                baseInfo.selfEvaluation === null) {
                baseInfo.selfEvaluation = "暂无数据";
            }
            $("#selfEvaluation").html(baseInfo.selfEvaluation);
            $("#email").html(baseInfo.email);
            $("#phone").html(baseInfo.phone);
            $("#city").html(baseInfo.city);


            // 工作经验
            $("#worksList").empty();
            if (profile.works.length <= 0) {
                $("#worksList").append(
                    "<li><a>暂无数据</a></li>"
                )
            } else {
                $.each(profile.works, function (i, work) {

                    let content = work.content;
                    if (content === null ||
                        content === "") {
                        content = "暂无内容";
                    }

                    $("#worksList").append(
                        "<div class='experience-item'>" +
                        "<div class='row'>" +
                        "<div class='col-lg-4'>" +
                        "<div class='content-left'>" +
                        "<h5 class='company-name theme-color'>企业名称: " + work.companyName + "</h5>" +
                        "<span class='date'>" + work.startDate + "&nbsp;~&nbsp;" + work.endDate + "</span>" +
                        "</div>" +
                        "</div>" +
                        "<div class='col-lg-8'>" +
                        "<div class='content-right'>" +
                        "<h5 class='position-company bottom'>职位: " + work.position + "</h5>" +
                        "<p>工作内容:</p>" +
                        "<p>" + content + "</p>" +
                        "</div>" +
                        "</div>" +
                        "</div>" +
                        "</div>"
                    )

                });
            }

            // 教育经历
            $("#educationList").empty();
            if (profile.educations.length <= 0) {
                $("#educationList").append(
                    "<li><a>暂无数据</a></li>"
                )
            } else {
                $.each(profile.educations, function (i, education) {

                    let content = education.experience;
                    if (content === null ||
                        content === "") {
                        content = "暂无内容";
                    }

                    $("#educationList").append(
                        "<div class='education-item'>" +
                        "<div class='row'>" +
                        "<div class='col-lg-4 col-md-4'>" +
                        "<div class='content-left'>" +
                        "<h5 class='speciality-school theme-color'>专业: " + education.major + "</h5>" +
                        "<span class='date'>" + education.startDate + "&nbsp;~&nbsp;" + education.endDate + "</span>" +
                        "</div>" +
                        "</div>" +
                        "<div class='col-lg-8 col-md-8'>" +
                        "<div class='content-right'>" +
                        "<h5 class='school-name bottom'>学校: " + education.school + "</h5>" +
                        "<p>教育经历:</p>" +
                        "<p>" + content + "</p>" +
                        "</div>" +
                        "</div>" +
                        "</div>" +
                        "</div>"
                    )

                });
            }


            // 项目经历
            let $projectsList = $("#projectsList");
            $projectsList.empty();
            if (profile.projects.length <= 0) {
                $projectsList.append(
                    "<li><a>暂无数据</a></li>"
                )
            } else {
                $.each(profile.projects, function (i, projects) {
                    let content = projects.content;
                    if (content === null ||
                        content === "") {
                        content = "暂无内容";
                    }
                    $projectsList.append(
                        "<div class='education-item'>" +
                        "<div class='row'>" +
                        "<div class='col-lg-4 col-md-4'>" +
                        "<div class='content-left'>" +
                        "<h5 class='speciality-school theme-color'>角色: " + projects.role + "</h5>" +
                        "<p class='date'>" + projects.startDate + "&nbsp;~&nbsp;" + projects.endDate + "</p>" +
                        "</div>" +
                        "</div>" +
                        "<div class='col-lg-8 col-md-8'>" +
                        "<div class='content-right'>" +
                        "<h5 class='school-name bottom'>项目链接: <a>" + projects.projectUrl + "</a></h5>" +
                        "<p>项目内容: </p>" +
                        "<p>" + content + "</p>" +
                        "</div>" +
                        "</div>" +
                        "</div>" +
                        "</div>"
                    )
                });
            }


            // 掌握技能
            let $skillsList = $("#skillsList");
            $skillsList.empty();
            if (profile.skills.length <= 0) {
                $skillsList.append(
                    "<li><a>暂无数据</a></li>"
                )
            } else {
                $.each(profile.skills, function (i, skills) {
                    $skillsList.append(
                        "<li><a>" + skills.skillName + "&nbsp;|&nbsp;" + skills.degree + "</a></li>"
                    )
                });
            }

            // 个人网站
            let $websitesList = $("#websitesList");
            $websitesList.empty();
            if (profile.websites.length <= 0) {
                $websitesList.append(
                    "<li><a>暂无数据</a></li>"
                )
            } else {
                $.each(profile.websites, function (i, websites) {
                    $websitesList.append(
                        "<li><a href='http://" + websites.websiteUrl + "'>" + websites.websiteUrl + "</a></li>"
                    )
                });
            }

        },
        error: function (err) {
            cocoMessage.error(2000, err.statusText, function () {
                console.log(err);
            });
        }
    })
}

function getInterestedList(){
    $.ajax({
        type: "GET",
        async: false,
        url: jobsServer + "/jobs/getListByStatus",
        // headers: {
        //     "Content-Type": "application/json"
        // },
        headers: {
            "Authorization": Authorization,
            "refreshToken": refreshToken,
        },
        data: {
            userId: CorHId,
            huntingStatus: "感兴趣"
        },
        success: function (data) {
            interestedList = data.datas;
            if ($.inArray(huntingId, interestedList) >= 0){
                $("#followBut i").attr("hidden","hidden");
                $("#followBut img").removeAttr("hidden");
                isInterested = true;
            }
        },
        error: function (err) {
            // 异常
            cocoMessage.error(2000, err.statusText, function () {
            });
        }
    })
}

function getInterviewList(){
    $.ajax({
        type: "GET",
        async: false,
        url: jobsServer + "/jobs/getListByStatus",
        // headers: {
        //     "Content-Type": "application/json"
        // },
        headers: {
            "Authorization": Authorization,
            "refreshToken": refreshToken,
        },
        data: {
            userId: CorHId,
            huntingStatus: "邀面试"
        },
        success: function (data) {
            interviewList = data.datas;
            if ($.inArray(huntingId, interviewList) >= 0){
                $(".shouCang").attr("hidden","hidden");
                $(".yaoMianShi").removeAttr("hidden");
            }
        },
        error: function (err) {
            // 异常
            cocoMessage.error(2000, err.statusText, function () {
            });
        }
    })
}

function updateHunting(status){

    if (status === "" || status === null){
        if (isInterested){
            status = "已查阅";
        }else {
            status = "感兴趣";
        }
    }
    $.ajax({
        type: "POST",
        async: false,
        url: jobsServer + "/jobs/huntingInfo",
        headers: {
            "Content-Type": "application/json",
            "Authorization": Authorization,
            "refreshToken": refreshToken,
        },
        data: JSON.stringify({
            "huntingStatus": status,
            "huntingId": huntingId,
        }),
        success: function (data) {
            if (data.resp_code === 0) {
                // 成功
                cocoMessage.info(2000, "操作成功", function () {
                });
                if (isInterested && status !== "邀面试"){
                    console.log("取消收藏")
                    $("#followBut img").attr("hidden","hidden");
                    $("#followBut i").removeAttr("hidden");
                    isInterested = false;
                }else if (!isInterested && status !== "邀面试") {
                    console.log("收藏")
                    $("#followBut i").attr("hidden","hidden");
                    $("#followBut img").removeAttr("hidden");
                    isInterested = true;
                }
            } else {
                // 失败
                cocoMessage.error(2000, "关注失败", function () {
                    console.log(data.resp_msg);
                });
            }
        },
        error: function (err) {
            // 异常
            cocoMessage.error(2000, err.statusText, function () {
                console.log(err);
            });
        }
    })
}
